package org.eduapp.trains.vo;

import java.net.URLDecoder;

import org.apache.struts.action.ActionForm;
import org.eduapp.trains.Constants;
import org.eduapp.trains.utils.UtilsString;

public class VoTrainSearch extends ActionForm {
	
	private static final long serialVersionUID = -4472469020181670708L;
	
	private String  trainSearchType;
	private String  trainNumber;
	private String  trainStationName;
	private String  trainStationFrom;
	private String  trainStationTo;
	private Integer trainOffset = 0;
	private Integer pageSize 	= Constants.DefaultPageSize;
	private Integer page		= 1;
	private String  order		= "train_number";
	private Boolean isDesc		= false;
	private String  query;
	
	public String getTrainNumber() {
		if (UtilsString.isNotEmpty(trainNumber)) {
			trainNumber = trainNumber.indexOf(Constants.DefaultTrainNumberEncode) > -1 ? 
					trainNumber.replaceAll(Constants.DefaultTrainNumberEncode, "/"): trainNumber;
		}
		return trainNumber;
	}
	public void setTrainNumber(String trainNumber) {
		this.trainNumber = trainNumber;
	}
	public void setTrainSearchType(String trainSearchType) {
		this.trainSearchType = trainSearchType;
	}
	public String getTrainSearchType() {
		return trainSearchType;
	}
	public void setIsDesc(Boolean isDesc) {
		this.isDesc = isDesc;
	}
	public Boolean getIsDesc() {
		return isDesc;
	}
	public void setTrainOffset(Integer trainOffset) {
		this.trainOffset = trainOffset;
	}
	public Integer getTrainOffset() {
		return trainOffset;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getOrder() {
		return order;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getPage() {
		return page;
	}
	public void setTrainStationName(String trainStationName) {
		this.trainStationName = trainStationName;
	}
	@SuppressWarnings("deprecation")
	public String getTrainStationName() {
		return UtilsString.isNotEmpty(trainStationName) ? URLDecoder.decode(trainStationName) : trainStationName;
	}
	public void setTrainStationFrom(String trainStationFrom) {
		this.trainStationFrom = trainStationFrom;
	}
	@SuppressWarnings("deprecation")
	public String getTrainStationFrom() {
		return UtilsString.isNotEmpty(trainStationFrom) ? URLDecoder.decode(trainStationFrom) : trainStationFrom;
	}
	public void setTrainStationTo(String trainStationTo) {
		this.trainStationTo = trainStationTo;
	}
	@SuppressWarnings("deprecation")
	public String getTrainStationTo() {
		return UtilsString.isNotEmpty(trainStationTo) ? URLDecoder.decode(trainStationTo) : trainStationTo;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public String getQuery() {
		if (UtilsString.isNotEmpty(trainSearchType)) {
			String tmpQuery = null;
			if (trainSearchType.equals("1") && UtilsString.isNotEmpty(trainNumber)) {
				tmpQuery = "?code=" + trainNumber.trim();
			} else if (trainSearchType.equals("2") && UtilsString.isNotEmpty(trainStationName)) {
				tmpQuery = "?station=" + trainStationName.trim();
			} else if (trainSearchType.equals("3") && 
					UtilsString.isNotEmpty(trainStationFrom) && UtilsString.isNotEmpty(trainStationTo)) {
				tmpQuery = "?from=" + trainStationFrom.trim() + "&to=" + trainStationTo.trim();
			}
			query = tmpQuery;
		}
		return query;
	}
	
}
